#include "NarfKeypointModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.narf_keypoint_model.json";
}

NarfKeypointModel::NarfKeypointModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/narf_keypoint_extraction.html");
}

NarfKeypointModel& NarfKeypointModel::get()
{
    static NarfKeypointModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void NarfKeypointModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    angularResolution_ = json["angularResolution"].toDouble(angularResolution_);
    supportSize_ = json["supportSize"].toDouble(supportSize_);
    coordinateFrame_ =
        static_cast<pcl::RangeImage::CoordinateFrame>(json["coordinateFrame"].toInt(static_cast<int>(coordinateFrame_))
        );
    setUnseenToMaxRange_ = json["setUnseenToMaxRange"].toBool(setUnseenToMaxRange_);
    noiseLevel_ = json["noiseLevel"].toDouble(noiseLevel_);
    minRange_ = json["minRange"].toDouble(minRange_);
    borderSize_ = json["borderSize"].toInt(borderSize_);
    this->keypointSize_ = json["keypointSize"].toInt(this->keypointSize_);
    this->keypointColor_ = QColor(json["keypointColor"].toString(this->keypointColor_.name()));
}

QJsonObject NarfKeypointModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("angularResolution", QJsonValue(angularResolution_));
    json.insert("supportSize", QJsonValue(supportSize_));
    json.insert("coordinateFrame", QJsonValue(static_cast<int>(coordinateFrame_)));
    json.insert("setUnseenToMaxRange", QJsonValue(setUnseenToMaxRange_));
    json.insert("noiseLevel", QJsonValue(noiseLevel_));
    json.insert("minRange", QJsonValue(minRange_));
    json.insert("borderSize", QJsonValue(borderSize_));

    json.insert("keypointSize", QJsonValue(this->keypointSize_));
    json.insert("keypointColor", QJsonValue(this->keypointColor_.name()));

    return json;
}
